﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace LuZhouLaoJiao
{
    public partial class RemoteBatchDetailsInfo : Form
    {
        public RemoteBatchDetailsInfo()
        {
            InitializeComponent();
        }
        public DataTable dt = new DataTable();
        MySqlConnection conn = null;
        MySqlCommand comm = null;
        MySqlDataReader reader = null;
        string strCon = null;
        string dbUser = Global.getRegeditValue("localDBUser");
        string dbPwd = Global.getRegeditValue("localDBPwd");
        string database = Global.getRegeditValue("localDatabase");
        string ip = Global.getRegeditValue("localIP");
        string pici_id = null;

        private void RemoteBatchDetailsInfo_Load(object sender, EventArgs e)
        {
            if (Global.inquiried)
            {
                RemoteInquiryResult inquiry = (RemoteInquiryResult)this.Owner;
                pici_id = inquiry.doublePiCi;
            }
            else
            {
                RemoteBatchInfo batchInfo = (RemoteBatchInfo)this.Owner;
                pici_id = batchInfo.doublePiCi;
            }
            
            strCon = "server = " + ip + "; user id = " + dbUser + "; password = " + dbPwd + "; database = " + database + "; persist security info = false";
            conn = new MySqlConnection(strCon);
            comm = new MySqlCommand();

            comm.Connection = conn;
            comm.CommandText = "select * from sample where pici_id = " + pici_id + ";";
            try
            {
                conn.Open();
                reader = comm.ExecuteReader();

                dt.Load(reader);
                dt.Columns[0].ColumnName = "样本编号";
                dt.Columns[1].ColumnName = "样本号";
                dt.Columns[2].ColumnName = "批次号";
                dt.Columns[3].ColumnName = "窖池号";
                dt.Columns[4].ColumnName = "酸度";
                dt.Columns[5].ColumnName = "机器号";
                dt.Columns[6].ColumnName = "检测时间";
                dt.Columns[7].ColumnName = "样本来源";
                dt.Columns[8].ColumnName = "备注";

                dataGridView1.DataSource = dt;
                dataGridView1.Columns[0].ReadOnly = true;
                dataGridView1.Columns[0].Visible = false;
                dataGridView1.Columns[1].ReadOnly = true;
                dataGridView1.Columns[2].Width = 120;
                dataGridView1.Columns[2].ReadOnly = true;
                dataGridView1.Columns[4].ReadOnly = true;
                dataGridView1.Columns[5].ReadOnly = true;
                dataGridView1.Columns[6].ReadOnly = true;
                this.Width = 870;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show("数据库读取错误(BDI)：" + ex.Message);
            }
            finally
            {
                reader.Close();
            }
        }

        private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
        {

            string header = dataGridView1.Columns[e.ColumnIndex].HeaderText;
            comm.CommandText = "update sample set " + Global.headerMatch(header) + " = '" + e.Value.ToString() + "' where pici_id = '" + pici_id + "';";
            try
            {
                comm.ExecuteNonQuery();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("数据库更新值错误(BDI)：" + ex.Message);
            }
        }

        private void BatchDetailsInfo_FormClosed(Object sender, FormClosedEventArgs e)
        {
            //关闭打开的数据库连接;
            conn.Close();
        }

        private void 输出到ExcelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult result = MessageBox.Show("确定要将本页数据输出到Excel?", "输出数据到Excel", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    Global.DataGridviewShowToExcel(dataGridView1, true);
                }
            }
            catch (System.Runtime.InteropServices.COMException)
            {
                MessageBox.Show("本机上没有Windows Office应用程序，无法使用此功能.");
            }
            
        }
    }
}
